package com.yhh.Entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.Date;
import java.util.concurrent.TimeUnit;

import static com.yhh.Entity.MyFileFactory.dfd;
import static com.yhh.Entity.MyFileFactory.dft;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class MyFile implements Serializable {

    //文件相对路径和名字
    private String filePath;

    //文件可供下载的次数
    private Integer counts = 1;

    //文件创建时间
    private Date create_time = new Date();

    //文件上次操作时间
    private Date update_time = new Date();

    //文件的key
    private String key;

    //文件上传的ip
    private String ip;

    //文件上次传输的ip
    private String lastIp;

    //time to live
    private Integer ttl;

    //timeunit
    private TimeUnit timeUnit;

    //文件描述
    private String content;

    //判断是否过期,应该抽离成工具类的静态方法
    public boolean isExpired() {
        if (this.ttl == null || this.timeUnit == null) return false;
        if (this.create_time.getTime() + timeUnit.toMillis(ttl) < new Date().getTime()) return true;
        return false;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder("key:" + key +
                " filename:" + filePath + " 剩余次数:" + counts +
                " 创建时间:" + dfd.format(create_time) + " " + dft.format(create_time) +
                " 上次传输时间:" + dfd.format(update_time) + " " + dft.format(update_time) +
                " 上次传输ip:" + lastIp);
        if (ttl != null) {
            Date date = new Date(create_time.getTime() + timeUnit.toMillis(ttl));
            sb.append(" 过期时间：" + dfd.format(date) + " " + dft.format(date));
        }
        return sb.toString();
    }
}
